package com.sinosoft.jwjw.component.controller.system;

import com.sinosoft.jwjw.base.BaseCrudController;
import com.sinosoft.jwjw.base.BaseCrudService;
import com.sinosoft.jwjw.component.model.system.SDataDictionaryCode;
import com.sinosoft.jwjw.component.service.system.SDataDictionaryCodeService;
import com.sinosoft.jwjw.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/sDataDictionaryCode")
public class SDataDictionaryCodeController  extends BaseCrudController<SDataDictionaryCode,String> {

    @Autowired
    private SDataDictionaryCodeService sDataDictionaryCodeService;


    @Override
    public BaseCrudService<SDataDictionaryCode, String> getBaseService() {
        return sDataDictionaryCodeService;
    }


    /**
     *获取代码类别所有的树   当不存在codeType的时候  我们查询代码类别表  代码表  如果存在codeType   我们只查询代码表
     * @return
     */
    @RequestMapping("/getAllTree")
    @ResponseBody
    public String getAllTree(){


        VoZtree ztree = null;
        List<VoZtree> ztreeList = new ArrayList<VoZtree>();
        //获取参数 返回的是params
        RequestParamUtils();
        String   codeTypePara =  ConvertUtil.toString(params.get("codeType"));
        ResultList resultList = new ResultList();
        try{
            //获取数据 返回给 ztreeList
            ztreeList = sDataDictionaryCodeService.getAllTree(codeTypePara);
            resultList.setCode(0);
            resultList.setMsg("查询成功");
            resultList.setResult(ConvertUtil.toJSONArray(ztreeList));
        }catch (Exception ee){
            resultList.setCode(500);
            resultList.setMsg("查询失败");
            resultList.setInnermsg(ee.toString());
        }finally{

        }


        return resultList.toString();
    }


    /**
     * 字典的查询  table列表
     */
    @RequestMapping("/pageQueryDictionNaryList")
    @ResponseBody
    public String pageQueryDictionNaryList(){
        //获取参数 返回的是params
        RequestParamUtils();
        String   id  =  ConvertUtil.toString(params.get("id"));
        int pageIndex = ConvertUtil.toInt(params.get("pageIndex"), 1);
        int pageSize = ConvertUtil.toInt(params.get("pageSize"), 20);
        Map<String,Object> map = new HashMap<String,Object>();
        ResultPage resultPage =   sDataDictionaryCodeService.pageQueryDictionNaryList(id,pageIndex,pageSize);
        return resultPage.toString();
    }

    /**
     * 新增 or 修改 字典
     * @return
     */
    @RequestMapping("/saveDataDictionary")
    @ResponseBody
    public  String saveDataDictionary(){
        RequestParamUtils();
        int result = sDataDictionaryCodeService.saveDataDictionary(params);
        return ConvertUtil.toString(result);
    }

    /**
     * 参数为id 和 codeProperty
     * @return
     */
    @RequestMapping("/deleteDataCode")
    @ResponseBody
    public String deleteDataCode(){
        RequestParamUtils();
        int result = sDataDictionaryCodeService.deleteDataCode(params);
        return ConvertUtil.toString(result);
    }
}

